//
//  FBCSViewController.m
//  VmiSJ
//
//  Created by Apple on 2017/1/10.
//  Copyright © 2017年 汪的一声就哭了！. All rights reserved.
//
#import "FBCSViewController.h"

@interface FBCSViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) UITableView *tableView;

@end

@implementation FBCSViewController
{
    CLLocationManager *_locationManager;
    NSMutableArray *_listArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"所在城市";
//    [self location];
    [self obtainListData];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = BackColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"WCSJTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
}

//设置单元格高度
- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}


//设置表视图中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _listArray.count;
}

//设置单元格中的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WCSJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    cell.label.text = [_listArray[indexPath.row] categoryname];
    cell.backImageView.image = [UIImage imageNamed:@"我的_箭头"];
    if ([[_listArray[indexPath.row] categoryname] isEqualToString:self.provinceStr]) {
        cell.backLabel.hidden = NO;
    } else {
        cell.backLabel.hidden = YES;
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.provinceStr = _listArray[indexPath.row];
    CityViewController *viewC = [[CityViewController alloc] init];
    viewC.province = self.provinceStr;
    viewC.cityStr = self.cityStr;
    viewC.province_id = [_listArray[indexPath.row] the_id];
    [self.navigationController pushViewController:viewC animated:YES];
}
- (void)location
{
    _locationManager = [[CLLocationManager alloc] init];
    [_locationManager requestWhenInUseAuthorization];
    [_locationManager requestWhenInUseAuthorization];
    //启动LocationService
    [_locationManager startUpdatingLocation];
    
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    CLGeocoder *revGeo = [[CLGeocoder alloc] init];
    [revGeo reverseGeocodeLocation:_locationManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (!error && [placemarks count] > 0)
        {
            NSDictionary *dict =
            [[placemarks objectAtIndex:0] addressDictionary];
            NSLog(@"%@",dict[@"State"]);
            NSLog(@"%@",dict[@"City"]);
        }
        else
        {
            NSLog(@"ERROR: %@", error);
        }
    }];
}
#pragma mark -------------- 发布城市网络请求
- (void)obtainListData
{
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"province"} HUD:YES block:^(id responseObject) {
        if ([responseObject[@"code"] isEqual:@200]) {
            _listArray = [NSMutableArray array];
            for (NSDictionary *dic in responseObject[@"result"]) {
                ProvinceModel *model = [[ProvinceModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_listArray addObject:model];
            }
            [self.tableView reloadData];
        } else {
            [[SQShowMessage shareMessage] showMessageWithStr:responseObject[@"msg"]];
        }
    }];
}

@end
